package cn.chenhao.pet.controller;

import cn.chenhao.basic.util.AjaxResult;
import cn.chenhao.basic.util.LoginContext;
import cn.chenhao.basic.util.PageList;
import cn.chenhao.pet.domain.Pet;
import cn.chenhao.pet.query.PetQuery;
import cn.chenhao.pet.service.IPetService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Api(tags = "宠物信息接口",description = "宠物信息接口接口详细描述")
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 分页和高级查询
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query){
        return petService.queryPage(query);
    }

    /**
     * 下架操作
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return AjaxResult.me();
        }
        catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setMessage("操作失败，晚点再来");
        }
    }


    /**
     * 上架操作
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return AjaxResult.me();
        }
        catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setMessage("操作失败，晚点再来");
        }
    }


    @GetMapping("/{id}")
    public Pet get(@PathVariable("id") Long id){
        return petService.findById(id);
    }

    /**
     * 领养
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/adopt/{id}")
    public AjaxResult adoptPet(@PathVariable("id") Long id, HttpServletRequest request){
        try {
            petService.adoptPet(id, LoginContext.currentLoginInfo(request).getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，请稍后再试！");
        }
    }

}
